package cn.openea.perpartner.sysuser.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.system.entity.SysUser;
import cn.openea.perpartner.system.entity.dto.UserCacheDto;
import cn.openea.perpartner.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Weird077
 * @date 2023-03-28 14:42
 */
@Api(tags = "用户缓存")
@RestController
@RequestMapping("/user")
public class UserCacheController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @ApiOperation(value = "初始用户缓存",notes = "初始化用户缓存到Redis")
    @GetMapping(value = "/cache/all")
    public TableDataInfo initUserCacheList() {
        List<UserCacheDto> list = userService.initUserCacheList();
        return getDataTable(list);
    }
    @ApiOperation(value="用户缓存列表",notes = "根据用户ID集合从缓存中查询用户信息")
    @PostMapping("/cache/list")
    public TableDataInfo selectUserCacheList(@RequestBody List<String> userIds) {
        List<UserCacheDto> list = userService.selectUserCacheList(userIds);
        return getDataTable(list);
    }

    @ApiOperation(value = "查询用户缓存",notes = "根据用户ID从缓存中查询用户信息")
    @GetMapping("/cache/{userId}")
    public AjaxResult selectUserCacheByUserId(@PathVariable String userId) {
        if (StrUtil.isBlank(userId)){
            return error("用户ID不能为空！");
        }
        UserCacheDto dto = userService.selectUserCacheByUserId(userId);
        return BeanUtil.isNotEmpty(dto)?success(dto):error("用户不存在！");
    }

    @ApiOperation(value = "清空用户缓存",notes = "清空Redis中所有用户缓存信息")
    @GetMapping("/cache/empty")
    public AjaxResult removeUserCache() {
        return userService.removeUserCache() ? success() : error();
    }

    @ApiOperation(value ="删除用户缓存",notes = "根据用户ID从缓存中删除用户信息")
    @GetMapping("/cache/remove/{userId}")
    public AjaxResult removeCacheByUserId(@PathVariable String userId) {
        if (StrUtil.isBlank(userId)){
            return error("用户ID不能为空！");
        }
        return userService.removeCacheByUserId(userId) ? success() : error();
    }

    @ApiOperation(value ="更新用户信息",notes = "更新用户信息,并更新缓存")
    @PostMapping("/update")
    public AjaxResult updateUserInfo(@RequestBody SysUser user) {
        return userService.updateUserInfo(user) ? success() : error();
    }
    @ApiOperation(value ="补全关注用户信息",notes = "补全关注用户信息")
    @GetMapping("/improveSubOpenid")
    public AjaxResult improveSubOpenid(){
        userService.improveSubOpenid();
        return AjaxResult.success();
    }
}
